<!-- guessnumber.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜数字游戏</title>
</head>
<body>
    <button id="resetBtn">重新开始一局游戏</button> <br>
    <span>请输入要猜的数字:</span>
    <input type="text">
    <button id="guessBtn">猜</button> <br>
    <span>已经猜的次数:</span><span id="guessCount">0</span><br>
    <span>结果:</span> <span id="result"></span>

    <script>
        //1.先把上面需要的元素都拿到
        let resetBtn = document.querySelector('#resetBtn');
        let input = document.querySelector('input');
        let guessBtn = document.querySelector('#guessBtn');
        let resultSpan = document.querySelector('#result');
        let guessCountSpan = document.querySelector('#guessCount');
        //2.生成1-100的随机整数.
        let toGuess = Math.floor(Math.random() * 100 + 1);
        console.log( toGuess );
        //3.实现点击猜数字的过程
         guessBtn.onclick = function(){
            //1.读取到input中输入的内容,并转成整数.
            if(input.value==''){
                return;
            }
            let curNum=parseInt(input.value);
            //判断大小关系并给出提示
            if(curNum < toGuess){
                resultSpan.innerHTML = '猜小了';
                resultSpan.style.color = 'red';
            }else if(curNum > toGuess){
                resultSpan.innerHTML = '猜大了';
                resultSpan.style.color = 'red';
            }else{
                resultSpan.innerHTML = '猜对了';
                resultSpan.style.color = 'green';
            }
            //3.更新猜的次数
            let guessCount = parseInt(guessCountSpan.innerHTML);
            guessCountSpan.innerHTML = guessCount + 1;
         }
         //4.实现reset操作的逻辑(开始新游戏)
         resetBtn.onclick=function(){
            //让页面进行刷新
            //location和document 并列关系的对象.
            //location用来控制页面的链接/地址  通过reload操作就可以刷新页面
            location.reload();
         }
       
    </script>
</body>
</html>
